package com.company.controller;

import com.company.model.Goods;
import com.company.service.GoodsService;
import com.company.utils.MessageResponse;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @GetMapping
    public List<Goods> findGoods(@RequestParam(value = "like", required = false) String like,
                                 @RequestParam(value = "type", required = false) String type) {
        if(like == null && type == null) {
            return GoodsService.findAllGoods();
        } else if(like == null && type != null) {
            return GoodsService.findByType(type);
        } else if(like != null && type == null) {
            return GoodsService.findLike(like);
        } else {
            // find, like and type not null
            return GoodsService.findByNameAndType(like, type);
        }
    }

    @PostMapping
    public MessageResponse<Boolean> insertGoods(@RequestBody Goods goods) {
        return GoodsService.insertGoods(goods);
    }

    @PutMapping
    public MessageResponse<Boolean> updateGoods(@RequestBody Goods goods) {
        return GoodsService.updateGoods(goods);
    }

    @DeleteMapping
    public MessageResponse<Boolean> deleteGoods(int id) {
        return GoodsService.deleteGoods(id);
    }
}
